package top.kaoshanji.p1ch01a;

import org.apache.hc.core5.http.ContentType;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 代码清单-p1ch01a-3页a：MyServlet类
 * URL访问地址：/my
 * @author kaoshanji
 * ^_^
 * create time 2025/4/6 19:25
 */
@WebServlet(name = "MyServlet", urlPatterns = {"/my"})
public class MyServlet implements Servlet {

    private transient ServletConfig servletConfig;

    @Override
    public void init(ServletConfig config) throws ServletException {
        this.servletConfig = config;
    }

    @Override
    public ServletConfig getServletConfig() {
        return servletConfig;
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        String servletName = servletConfig.getServletName();

        // 设置响应对象的编码和内容格式
        res.setContentType(ContentType.TEXT_HTML.toString());
        PrintWriter out = res.getWriter();

        out.print("<html><head></head>"
                + "<body>Hello from " + servletName
                + "</body></html>");

    }

    @Override
    public String getServletInfo() {
        return "My Servlet";
    }

    @Override
    public void destroy() {

    }



}
